from django.shortcuts import render
from django.http import HttpResponse
# Create your views here.
from django.urls import reverse
from book.models import BookInfo, PersonInfo


def index(req):
    # return HttpResponse('Hey index')

    name = "Tom"
    age = 18
    context = {
        'name': name,
        'Age': age
    }

    # book = BookInfo.objects.all()
    #
    # context = {
    #     'book': book
    # }

    path = reverse('book:index')
    print(path)

    return render(req, 'index.html', context=context)


def detail(req, category_id, book_id):
    # reverse('book:detail')  # # 这样起别名，会报参数个数的错
    # print(category_id)
    # print(book_id)

    query_params = req.GET
    print(query_params)  # <QueryDict: {'name': ['laowang'], 'age': ['18']}>
    """
    又。。。错啦
    # username = query_params.name
    # age = query_params.age
    # print(username)
    # print(age)
    """
    username = query_params.get('name')
    username_list = query_params.getlist('name')
    age = query_params.get('age')
    print(username)
    print(username_list)
    print(age)

    return HttpResponse('街里detail')


"""
############# 关联查询

# 查询书籍为1 的所有人物信息
book  = BookInfo.objects.get(id=1)
person = PersonInfo.objects.filter(book=1)  # 哈哈自己想的办法也行
person01 = book.personinfo_set.all()  # 老师给的方法

# 查询人物为1的书籍信息

person2 = PersonInfo.objects.get(id=1)
book2 = person2.book
name2 = person2.name

# 查询图书 要求图书人物为郭靖
BookInfo.objects.filter(personinfo__name__exact='郭靖')

book = BookInfo.objects.get(id=1)
book.personinfo_set.all()
# 无缓存
[book.id for book in BookInfo.objects.all()]

books = BookInfo.objects.all()
[book.id for book in books]
"""
